<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/28
 * Time: 17:30
 */

namespace addons\roll\controller;
use app\common\controller\Addon;

use addons\roll\model\RollprizeModel;

use think\Controller;
use think\Db;
use think\facade\Request;

class Rollprize extends Addon
{
    private $rollModel;
    public function __construct()
    {
        parent::__construct();
        $this->rollModel = new RollprizeModel();
    }

    public function index(){
        $get = Request::get();
        $list = Db::table('rh_roll_prize')->where(['roll_id'=>$get['roll_id']])->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 保存转盘奖品
     * @return \think\response\Json
     */
    public function addRollPrize(){
        $post = Request::post();
        $info = Db::table('rh_roll_prize')->where(['user_mobile'=>$post['user_mobile'],'roll_id'=>$post['roll_id']])->select();
        if($info){
            return json(['code'=>200,'msg'=>'您已经获得过奖品了，请耐心等待工作人员联系。']);
        }
        $re = $this->rollModel->addPrize($post);
        if($re === false){
            return json(['code'=>500,'msg'=>'保存奖品失败']);
        }
        return json(['code'=>200,'msg'=>'提交成功，请耐心等待工作人员联系']);
    }

    /**
     * 获取获奖列表
     * @return \think\response\Json
     */
    public function getPrizeList(){
        //判断请求方式以及请求参数
        $inputData = Request::get();
        $method = Request::method();
        $params = ['member_id'];
        $ret = checkBeforeAction($inputData, $params, $method, 'GET', $msg);
        if(!$ret){
            return reJson(500, $msg, []);
        }

        //获取获奖列表
        $condition['member_id'] = $inputData['member_id'];
        $field = 'member_id, roll_prize, create_time';
        $list = $this->rollModel->getPrizeList($condition, $field);
        if($list === false){
            return reJson(500, '获取列表失败', []);
        }
        foreach ($list as $k => &$v){
            $v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        }

        return reJson(200, '获取列表成功', $list);
    }

    /**
     * 设置奖品
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function prize($roll_id = ''){
        $get = Request::get();
        $roll_id = $roll_id ? $roll_id : $get['id'];
        $prize = Db::table('rh_roll_set_prize')->where(['roll_id' => $roll_id])->select();

        $this->assign('prize', $prize);
        $this->assign('roll_id', $roll_id);
        return $this->fetch('prize');
    }

    public function delete(){
        $get = Request::get();
        Db::table('rh_roll_set_prize')->where(['prize_id' => $get['id']] )->delete();
        $this->assign('roll_id', $get['roll_id']);
        $this->prize($get['roll_id']);
    }

    public function setPrize(){
        $data = Request::post();
        
        $total = 0;
        foreach ($data as $k =>$v){
            $v[1] = sprintf('%.2f',str_replace('%','',$v[1]));

            $total += $v[1];
            $data[$k][1] = $v[1];
        }
        if($total != 100){
            return json(array('code'=>500,'msg'=>'几率总数相加必须为100%'));
        }
        Db::startTrans();
        foreach ($data as $k => $v){
            $re = Db::table('rh_roll_set_prize')->where(['prize_id' => $k])->update(['prize_name' => $v[0],'odds' =>$v[1]]);
            if($re === false){
                Db::rollback();
                return json(array('code'=>500,'msg'=>'设置失败'));
            }
        }
        Db::commit();
        return json(array('code'=>200,'msg'=>'设置成功'));
    }

    public function addPrize(){
        if(Request::method() == 'POST'){
            $post = Request::post();
            if(Db::table('rh_roll_set_prize')->insert($post)){
                $this->prize($post['roll_id']);
            }else{
                return json(array('code'=>500,'msg'=>'设置失败'));
            }
        }else{
            $get = Request::get();
            $this->assign('roll_id', $get['roll_id']);
            return $this->fetch();
        }
    }
}